package com.ymatou.iapi.couponmanage.testcase;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.parameter.ChangeOneBatchTimeInActivityBean;
import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.service.ChangeOneBatchTimeInActivityCall;
import com.ymatou.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.couponbatchWapper;
/**
*平台活动中，运营人员选择或取消买手券
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_changeOneBatchTimeInActivityBatch{
	private static ChangeOneBatchTimeInActivityBean bean;
	private static ChangeOneBatchTimeInActivityCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("平台活动中，运营人员选择或取消买手券");
	}

	@Before
	public void caseUp() {
		bean=new ChangeOneBatchTimeInActivityBean();
		call=new ChangeOneBatchTimeInActivityCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changeOneBatchTimeInActivity_001() {
		Logger.start(true,"正例-平台活动中，运营人员选择或取消买手券");
		try {
			// 活动优惠券
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			SellerCreateBatchBean sellercreatebatchBean = CouponServiceMCallServiceV2.setDefaultSellerCreateBatch_ActivityBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponServiceMCallServiceV2.SellerCreateBatch(sellercreatebatchBean);
			
			Date dNow = new Date(); // 当前时间
			Date end = new Date();

			Calendar calendar2 = Calendar.getInstance(); // 得到日历
			calendar2.setTime(dNow);// 把当前时间赋给日历
			calendar2.add(Calendar.HOUR, -1); // 
			end = calendar2.getTime(); // 得到前一天的时间
			
			bean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			bean.setAcquireEndTime(end);
            bean.setValidEnd(end);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("0",call.getCode(),"验证返回的statusCode");
			Logger.verifyEquals(true,call.getMessage().contains("操作成功！"),"验证返回的Message");
			//验证数据库
			 SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms"); 
			couponbatch batch=new couponbatchWapper().selectBybatchCode(bean.getBatchCode()).get(0);
			Logger.verifyEquals(time.format(batch.getAcquireEndTime()), time.format(bean.getAcquireEndTime()), "验证修改的AcquireEndTime字段");
			Logger.verifyEquals(time.format(batch.getValidEnd()), time.format(bean.getValidEnd()), "验证修改的ValidEnd字段");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changeOneBatchTimeInActivity_002() {
		Logger.start(true,"正例-平台活动中，运营人员选择或取消买手券,验证batchCode为空，返回未找到对应的优惠券批次");
		try {

			Date dNow = new Date(); // 当前时间
			Date end = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -1); // 
			end = calendar1.getTime(); // 得到前一天的时间
			
			bean.setBatchCode("11");
			bean.setAcquireEndTime(end);
            bean.setValidEnd(end);
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("2",call.getCode(),"验证返回的statusCode");
			Logger.verifyEquals(true,call.getMessage().contains("未找到对应的优惠券批次"),"验证返回的Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changeOneBatchTimeInActivity_003() {
		Logger.start(true,"正例-平台活动中，运营人员选择或取消买手券,验证不是买手活动优惠券，返回只能针对平台活动优惠券做此操作");
		try {
			// 活动优惠券
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			SellerCreateBatchBean sellercreatebatchBean = CouponServiceMCallServiceV2.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponServiceMCallServiceV2.SellerCreateBatch(sellercreatebatchBean);
			
			Date dNow = new Date(); // 当前时间
			Date end = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -1); // 
			end = calendar1.getTime(); 

			bean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			bean.setAcquireEndTime(end);
            bean.setValidEnd(end);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("2",call.getCode(),"验证返回的statusCode");
			Logger.verifyEquals(true,call.getMessage().contains("只能针对平台活动优惠券做此操作"),"验证返回的Message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changeOneBatchTimeInActivity_004() {
		Logger.start(true,"正例-平台活动中，运营人员选择或取消买手券,验证BatchCode为空，返回错误");
		try {

			Date dNow = new Date(); // 当前时间
			Date end = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -1); // 
			end = calendar1.getTime(); // 得到前一天的时间
			
			//bean.setBatchCode("11");
			bean.setAcquireEndTime(end);
            bean.setValidEnd(end);
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("1",call.getCode(),"验证返回的statusCode");
			Logger.verifyEquals(true,call.getMessage().contains("batchCode must"),"验证返回的Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changeOneBatchTimeInActivity_005() {
		Logger.start(true,"正例-平台活动中，运营人员选择或取消买手券,验证acquireEndTime为空，返回失败");
		try {
			// 活动优惠券
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			SellerCreateBatchBean sellercreatebatchBean = CouponServiceMCallServiceV2.setDefaultSellerCreateBatch_ActivityBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponServiceMCallServiceV2.SellerCreateBatch(sellercreatebatchBean);
			
			Date dNow = new Date(); // 当前时间
			Date end = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.HOUR, -1); // 
			end = calendar1.getTime(); // 得到前一天的时间
			
			bean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			//bean.setAcquireEndTime(end);
            bean.setValidEnd(end);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("1",call.getCode(),"验证返回的statusCode");
			Logger.verifyEquals(true,call.getMessage().contains("acquireEndTime"),"验证返回的Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changeOneBatchTimeInActivity_006() {
		Logger.start(true,"正例-平台活动中，运营人员选择或取消买手券,验证validEnd为空，返回失败");
		try {
			// 活动优惠券
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			SellerCreateBatchBean sellercreatebatchBean = CouponServiceMCallServiceV2.setDefaultSellerCreateBatch_ActivityBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponServiceMCallServiceV2.SellerCreateBatch(sellercreatebatchBean);
			
			Date dNow = new Date(); // 当前时间
			Date end = new Date();

			Calendar calendar2 = Calendar.getInstance(); // 得到日历
			calendar2.setTime(dNow);// 把当前时间赋给日历
			calendar2.add(Calendar.HOUR, -1); // 
			end = calendar2.getTime(); // 得到前一天的时间
			
			bean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			bean.setAcquireEndTime(end);
            //bean.setValidEnd(validEnd);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("1",call.getCode(),"验证返回的statusCode");
			Logger.verifyEquals(true,call.getMessage().contains("validEnd"),"验证返回的Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}